﻿using Newtonsoft.Json;
using socketio.socketio;
using SocketioTest.Common;
using SocketioTest.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using static socketio.socketio.Data;

namespace SocketioTest.View
{
    /// <summary>
    /// UsresList.xaml 的交互逻辑
    /// </summary>
    public partial class UsresList : Page
    {
      
        public UsersListViewModel usersListViewModel = new UsersListViewModel();

        public delegate void SelectdUsersDelegate();
        public event SelectdUsersDelegate selectdUsersEvent;

        public UsresList()
        {
            InitializeComponent();
            DataContext = usersListViewModel;
            Socketa.client.On("loginSuccess", response =>  // 用户上线报告
            {
                string text = response.GetValue<string>();
                this.ReceivedUsersListEvent(text);
            });
        }


        public void ReceivedUsersListEvent(string objjson)
        {
            List<UsersListViewModel> userLists = JsonConvert.DeserializeObject<List<UsersListViewModel>>(objjson);

            for (int i = userLists.Count - 1; i >= 0; i--)
            {
                if (userLists[i].userId == Global.uid)
                {
                    userLists.Remove(userLists[i]);
                }
            }
            ImageListBox.Dispatcher.Invoke(new Action(() =>
            {
                ImageListBox.ItemsSource = userLists;
            }));
        }
        
        private void SelectdUsersChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = (ListBox)sender;
            var country = (UsersListViewModel)item.SelectedItem;
           
            MainWindowViewModel.GetInstance().SelectdUsersChanged = country;
            //Console.WriteLine(JsonConvert.SerializeObject(MainWindowViewModel.GetInstance().SelectdUsersChanged));
            this.selectdUsersEvent();
        }
    }
}
